package org.example.aienglishapp.controller.progress;


import lombok.Getter;
import lombok.Setter;
import org.example.aienglishapp.entity.progress.Course;
import org.example.aienglishapp.service.progress.CourseService;
import org.example.aienglishapp.service.progress.UserProgressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class CourseController {

    private final CourseService courseService;


    @Autowired
    public CourseController(CourseService courseService, UserProgressService userProgressService){
        this.courseService = courseService;
    }



    // 获取可选课程
    @GetMapping("/available-courses/{courseId}")
    public ResponseEntity<List<Course>> getAvailableCourses(@PathVariable Long courseId){
        List<Course> courses = courseService.getAvailableCourses(courseId);
        return new ResponseEntity<>(courses, HttpStatus.OK);
    }

    // 获取已选课程
    @GetMapping("/enrolled-courses/{userId}")
    public ResponseEntity<List<Course>> getEnrolledCourses(@PathVariable Long userId){
        List<Course> courses = courseService.getEnrolledCourses(userId);
        return new ResponseEntity<>(courses, HttpStatus.OK);
    }

    // 选课
    @PostMapping("/enroll-course/{userId}/{courseId}")
    public ResponseEntity<String> enrollCourse(@PathVariable Long userId,@PathVariable Long courseId){
        try{
            courseService.enrollCourse(userId,courseId);
            return new ResponseEntity<>("选课成功", HttpStatus.OK);
        } catch(Exception e){
            return new ResponseEntity<>("选课失败: " + e.getMessage(), HttpStatus.BAD_REQUEST);
        }
    }
    @DeleteMapping("/enroll-course/{userId}/{courseId}")
    public ResponseEntity<String> deleteEnrolledCourse(@PathVariable Long userId, @PathVariable Long courseId) {
        try {
            courseService.deleteEnrollment(userId, courseId);
            return new ResponseEntity<>("删除选课成功", HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>("删除选课失败: " + e.getMessage(), HttpStatus.BAD_REQUEST);
        }
    }

    // 选课请求体类
    @Setter
    @Getter
    public static class EnrollRequest {
        private Long courseId;

    }
}